package com.brillilab.service.core.space.impl;

import java.util.List;

import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.enums.space.DeviceType;
import com.brillilab.domain.vo.lock.ItemLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.space.DeviceMapper;
import com.brillilab.dao.mapper.system.IdUtilMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.enums.system.IdUtilTypeEnum;
import com.brillilab.domain.po.lab.LabDevice;
import com.brillilab.domain.po.space.Device;
import com.brillilab.domain.po.system.IdUtil;
import com.brillilab.domain.vo.inventory.PanelInventoryCount;
import com.brillilab.domain.vo.space.PositionInfo;
import com.brillilab.service.core.lab.ILabDeviceService;
import com.brillilab.service.core.space.IDeviceService;

/**
 * @author wuzhiyu
 * @Title: DeviceServiceImpl
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2018/11/1310:16
 */
@Service
@Transactional
public class DeviceServiceImpl implements IDeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private IdUtilMapper idUtilMapper;
    @Autowired
    private ILabDeviceService labDeviceService;

    @Override
    public Device add(Device device) {
        deviceMapper.insert(device);
        return device;
    }

    @Override
    public boolean insertNotUserDeivce(List<Device> list) {
        Assert.isTrue(deviceMapper.insertNotUseDeviceList(list) > 0, "保存失败");
        return true;
    }

    @Override
    public Integer deviceCount(Long roomId) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Device::getRoomId, roomId).eq(Device::getIsDelete, 0);
        return deviceMapper.selectCount(wrapper);
    }

    @Override
    public Long getNewGroupId() {
        IdUtil idUtil = new IdUtil();
        idUtil.setName(IdUtilTypeEnum.DEVICE_GROUP.getType());
        idUtilMapper.insert(idUtil);
        return idUtil.getId();
    }

    @Override
    public Device get(Long deviceId) {
        return deviceMapper.selectById(deviceId);
    }

    @Override
    public boolean delete(Long deviceId) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Device::getId, deviceId);
        Device device = new Device();
        device.setIsDelete(1);
        return deviceMapper.update(device, wrapper) > 0;
    }

    @Override
    public boolean deleteByRoomId(Long roomId) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Device::getRoomId, roomId);
        Device device = new Device();
        device.setIsDelete(1);
        return deviceMapper.update(device, wrapper) > 0;
    }

    @Override
    public boolean update(Device device) {
        return deviceMapper.updateById(device) > 0;
    }

    @Override
    public boolean updatePosition(List<PositionInfo> list) {
        list.forEach(o -> {
            Device device = new Device();
            device.setId(o.getId());
            device.setHeight(o.getHeight());
            device.setWidth(o.getWidth());
            device.setX(o.getX());
            device.setY(o.getY());
            deviceMapper.updateById(device);
        });
        return true;
    }


    @Override
    public List<PanelInventoryCount> getPanelInventoryCount(Long deviceId) {
        return deviceMapper.getPanelInventoryCountList(deviceId);
    }

    @Override
    public List<Device> list(Long roomId) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Device::getRoomId, roomId);
        wrapper.eq(Device::getIsDelete, IsDeleteEnum.NO.getValue());
        return deviceMapper.selectList(wrapper);
    }

    @Override
    public List<Device> getLabAllDeviceList(Long labId, Long roomId) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Device::getLabId, labId);
        wrapper.isNotNull(Device::getDeviceIndex);
        wrapper.eq(Device::getIsDelete, IsDeleteEnum.NO.getValue());
        if (roomId != null) {
            wrapper.eq(Device::getRoomId, roomId);
        }
        return deviceMapper.selectList(wrapper);
    }

    @Override
    public boolean updateRoomName(Long roomId, String name) {
        Device device = new Device();
        device.setRoomId(roomId);
        device.setRoomName(name);
        LambdaQueryWrapper<Device> query = new LambdaQueryWrapper<>();
        query.eq(Device::getRoomId, roomId);
        deviceMapper.update(device, query);
        return true;
    }

    @Override
    public List<Device> listByAioDeviceNo(String aioDeviceNo) {
        LabDevice labDevice = labDeviceService.selectByNo(aioDeviceNo);
        Assert.notNull(labDevice, "一体机编号错误！");
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Device::getAioDeviceId, labDevice.getId());
        wrapper.eq(Device::getIsDelete, IsDeleteEnum.NO.getValue());
        return deviceMapper.selectList(wrapper);
    }

    @Override
    public List<ItemLock> selectDeviceLockMapByInvId(List<Long> invIds) {
        Assert.isTrue(invIds != null && invIds.size() > 0, "参数缺失！");
        return deviceMapper.selectDeviceLockMapByInvIds(invIds);
    }

    @Override
    public List<ItemLock> selectDeviceLockMapByUsageId(List<Long> usageIds) {
        Assert.isTrue(usageIds != null && usageIds.size() > 0, "参数缺失！");
        return deviceMapper.selectDeviceLockMapByUsageIds(usageIds);
    }

    @Override
    public List<Device> listDeviceGroupId(Long groupId) {
        LambdaQueryWrapper<Device> query = new LambdaQueryWrapper<>();
        query.eq(Device::getGroupId, groupId);
        query.eq(Device::getIsDelete, BoolEnum.FALSE.getValue());
        return deviceMapper.selectList(query);
    }

    @Override
    public List<Device> getAioDevice(Long roomId) {
        LambdaQueryWrapper<Device> query = new LambdaQueryWrapper<>();
        query.eq(Device::getDeviceType, DeviceType.MACHINE.getCode());
        query.eq(Device::getIsDelete, BoolEnum.FALSE.getValue());
        query.eq(Device::getRoomId, roomId);

        return deviceMapper.selectList(query);
    }

    @Override
    public List<Long> getAioBindRoomIdList(Long aioLabDeivceId) {
        return deviceMapper.getAioBindRoomIdList(aioLabDeivceId);
    }
}
